/**
 * author:Administrator
 * date:2011-9-7
 */
package com.roc.page;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.util.Constance;
import com.roc.util.constant.AgentType;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Role;

/**
 * @author Administrator
 * 
 */
public abstract class BaseAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8769497665756626162L;

	/**
	 * Constant for cancel result String
	 */
	public static final String CANCEL = "cancel";
	public static final String MODIFY = "modify";
	public static final String MODIFYBATCH = "modifyBatch";
	public static final String DOWNLOAD = "download";
	public static final String CREATE = "create";
	public static final String QUERY = "query";

	// public static final String ATHORITY="athority";

	private String moduleCode;

	/**
	 * Convenience method to get the request
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * Convenience method to get the response
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * Convenience method to get the session.
	 * 
	 * @return the session from ActionContext.
	 */
	protected HttpSession getSession() {
		/*
		 * if(ActionContext.getContext()==null){ return null; } return
		 * ActionContext.getContext().getSession();
		 */
		return getRequest().getSession();
	}
	
	protected Map<String, String> dealRoleType(Operators operators,Map<String, String> roleTypes){
		Map<String, String> roleTypesMap=new TreeMap<String, String>();
		roleTypesMap.clear();
		roleTypesMap.putAll(roleTypes);
		
		roleTypesMap.remove(Role.MANAGER.getCode());
		roleTypesMap.remove(Role.MARKET_DIRECT.getCode());
		roleTypesMap.remove(Role.MARKET_LEAD.getCode());
		roleTypesMap.remove(Role.FINANCE_COMM.getCode());
		roleTypesMap.remove(Role.FINANCE_LEAD.getCode());
		roleTypesMap.remove(Role.SFB_AGENT.getCode());
		if(Role.O_MANAGER.getCode().equals(operators.getRoleType())){
			roleTypesMap.clear();
			roleTypesMap.put(Role.OPERATORS_COMM.getCode(), Role.OPERATORS_COMM.getName());
			roleTypesMap.put(Role.AGENT_BUSS.getCode(), Role.AGENT_BUSS.getName());
		}else if(Role.SYSTEM.getCode().equals(operators.getRoleType())){
			roleTypesMap.remove(Role.AGENT_BUSS.getCode());
			roleTypesMap.remove(Role.O_MANAGER.getCode());
			roleTypesMap.remove(Role.MERCHANT.getCode());
			roleTypesMap.remove(Role.AGENT.getCode());
			roleTypesMap.remove(Role.SFB_AGENT.getCode());
		}else if(Role.AGENT.getCode().equals(operators.getRoleType())){
			roleTypesMap.clear();
			roleTypesMap.put(Role.AGENT_BUSS.getCode(), Role.AGENT_BUSS.getName());
		}else if(Role.OPERATORS_COMM.getCode().equals(operators.getRoleType())||Role.OPERATORS_LEAD.getCode().equals(operators.getRoleType())){
			if("48502000".equals(operators.getOneagentid())){
				roleTypesMap.remove(Role.AGENT_BUSS.getCode());
				roleTypesMap.remove(Role.O_MANAGER.getCode());
				roleTypesMap.remove(Role.MERCHANT.getCode());
				roleTypesMap.remove(Role.AGENT.getCode());
				roleTypesMap.remove(Role.SFB_AGENT.getCode());
			}else{
				roleTypesMap.clear();
				roleTypesMap.put(Role.AGENT_BUSS.getCode(), Role.AGENT_BUSS.getName());
			}
		}	
		return roleTypesMap;
	}
	protected Map<String, String> dealAgentType(String flag,String agentType,Map<String, String> agentTypes){
		Map<String, String> agentTypesMap=new TreeMap<String, String>();
		
		Map<String, String> map=ConfigProperties.getAgentTypeProperties();
		agentTypesMap.clear();
		agentTypesMap.putAll(map);
		if(flag.equals(Constance.OPERATORS_FLAG_OEM)){
			agentTypesMap.remove(AgentType.OEM.getCode());
			if(AgentType.CHINA.getCode().equals(agentType)){
				agentTypesMap.remove(AgentType.CHINA.getCode());
			}
			if(AgentType.PROVINCE.getCode().equals(agentType)){
				agentTypesMap.remove(AgentType.CHINA.getCode());
				agentTypesMap.remove(AgentType.PROVINCE.getCode());
			}
			if(AgentType.CITY.getCode().equals(agentType)){
				agentTypesMap.remove(AgentType.CHINA.getCode());
				agentTypesMap.remove(AgentType.PROVINCE.getCode());
				agentTypesMap.remove(AgentType.CITY.getCode());
			}
		}
		return agentTypesMap;
	}
	/**
	 * 根据机构号得到角色
	 * @param orgCode
	 * @param roleTypes
	 * @return
	 */
	protected Map<String, String> dealRoleTypeByOrgCode(String roleTypeStr,String orgCodeOld,String orgCode,Map<String, String> roleTypes){
		Map<String, String> roleTypesMap=new TreeMap<String, String>();
		roleTypesMap.clear();
		roleTypesMap.putAll(roleTypes);
		
		if(roleTypeStr.equals(Role.SYSTEM.getCode())){
			if(orgCodeOld.equals(orgCode)){
				roleTypesMap.remove(Role.MERCHANT.getCode());
				roleTypesMap.remove(Role.AGENT.getCode());
			}else{
				roleTypesMap.clear();
				roleTypesMap.put(Role.O_MANAGER.getCode(), Role.O_MANAGER.getName());
			}
		}
		return roleTypesMap;
	}
	protected List<OrgBean> dealOrgCode(String roleCode,List<OrgBean> orgList){
		return null;
	}

	public void initSomeDataForAction() {

	}

	/**
	 * @return the moduleCode
	 */
	public String getModuleCode() {
		return moduleCode;
	}

	/**
	 * @param moduleCode
	 *            the moduleCode to set
	 */
	public void setModuleCode(String moduleCode) {
		this.moduleCode = moduleCode;
	}

	protected int random;

	public BaseAction() {
		random = Math.abs(new java.util.Random().nextInt()) % 900000 + 100000;
	}

	public int getRandom() {
		random = Math.abs(new java.util.Random().nextInt()) % 900000 + 100000;
		return random;
	}

}
